import { Mesh, MeshStandardMaterial, Group, Color, MeshPhysicalMaterial, DoubleSide, PlaneGeometry } from "three";
import { App, Entity, RosourceItemsReturnType, ShadertoyMaterial, flatModel, createReflectorShaderMesh } from "vjmap3d";
// @ts-ignore
import runbg_glsl from "./glsl/runbg.glsl"
export const loadCarModel = (app: App, allResources: RosourceItemsReturnType) => {
    let modelCar = allResources.getModel("sm_car")
    modelCar.addTo(app);

    let models = flatModel(modelCar);
    models.forEach((item) => {
        // @ts-ignore
        if (item.isMesh) {
            // @ts-ignore
          const mat = item.material as MeshStandardMaterial;
          mat.aoMap = allResources.getTexture("ut_car_body_ao");
        }
    });
  
    const Wheel = models[36] as Group;
    let wheelEntity = Entity.attchObject(Wheel);
    return {
        carEntity: modelCar,
        wheelEntity
    }
}

export const loadPlane = (app: App, allResources: RosourceItemsReturnType) => {
    let modelPlane = allResources.getModel("sm_startroom")
    modelPlane.addTo(app);

    let models = flatModel(modelPlane);
    const light001 = models[2] as Mesh;
    const lightMat = light001.material as MeshStandardMaterial;
    lightMat.emissive = new Color("white");
    lightMat.emissiveIntensity = 1;
    lightMat.toneMapped = false;
    lightMat.transparent = true;
    lightMat.alphaTest = 0.1;
    lightMat.emissive.set(new Color("#ffffff"));
    lightMat.emissiveIntensity = 1;

    const ReflecFloor = models[3] as Mesh;
    const floorMat = ReflecFloor.material as MeshPhysicalMaterial;
    floorMat.aoMap = allResources.getTexture("ut_startroom_ao");
    floorMat.lightMap = allResources.getTexture("ut_startroom_light");
    floorMat.normalMap = allResources.getTexture("ut_floor_normal");
    floorMat.roughnessMap = allResources.getTexture("ut_floor_roughness");
    floorMat.envMapIntensity = 0;
    floorMat.transparent = true;
    floorMat.opacity = 0.4
    
    // setMeshReflector(ReflecFloor, {
    //     ignoreObjects: [light001],
    //     intensity: 100
    //   })

    const geometry = new PlaneGeometry(1000, 1000)
 
    const { mesh } = createReflectorShaderMesh(geometry, {
        reflectorMaterialType: "ReflectorDudvMaterial",
        reflectivity: 0.2,
    }, [light001])
    mesh.position.y = 0.1
    mesh.rotation.x = -Math.PI / 2
    app.scene.add(mesh)
}


export const loadCarRun = (app: App, allResources: RosourceItemsReturnType) => {
    let runCar = allResources.getModel("sm_speedup")
    runCar.addTo(app);

    let models = flatModel(runCar)
    
    let material = new ShadertoyMaterial(app, {
        resolution: [app.containerSize.width, app.containerSize.height],
        shader: runbg_glsl,
        // @ts-ignore
        transparent: false,
        side: DoubleSide
    });

    const carMesh = models[1] as Mesh;
    let runCarEntity = Entity.attchObject(carMesh, true, true)
    return {
        runCar: runCar,
        runCarEntity,
        runCarMaterial: material
    }
}